package org.wheel.base.utils;

import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.SystemPropertyUtils;

import java.util.Map;
import java.util.Properties;

/**
 * @author klaus
 * @date 2021/4/7 华小勇 10:32
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    protected static final PropertyPlaceholderHelper HELPER = new PropertyPlaceholderHelper(
            SystemPropertyUtils.PLACEHOLDER_PREFIX, SystemPropertyUtils.PLACEHOLDER_SUFFIX, SystemPropertyUtils.VALUE_SEPARATOR, true);

    /**
     * 字符串占位符处理
     * ${name}
     * @param string
     * @param properties
     * @return
     */
    public static String replacePlaceholders(String string, Properties properties) {
        return HELPER.replacePlaceholders(string, properties);
    }

    /**
     * 字符串占位符处理
     * ${name}
     * @param string
     * @param parameter
     * @return
     */
    public static String replacePlaceholders(String string, Map<String, Object> parameter) {
        return HELPER.replacePlaceholders(string, (name) -> String.valueOf(parameter.getOrDefault(name, "")));
    }

}
